import random


class util:
    @staticmethod
    def str_to_hex(s):
        return ' '.join([hex(ord(c)).replace('0x', '') for c in s])

    @staticmethod
    def get_sum(buff):
        j = 0
        for i in range(2, 18):
            j = j + buff[i]
        return j

    @staticmethod
    def str_translate(row):
        hexbyte = []
        row_in = [x for x in row]
        for i in range(row_in.__len__()):
            if row_in[i] == '':
                row_in[i] = '0'

        hexbyte.append(0xaa)
        hexbyte.append(0xaa)
        hexbyte.append(int(row_in[3], 16) % 256)
        hexbyte.append(int(row_in[3], 16) // 256)
        hexbyte.append(0x00)
        hexbyte.append(0x00)
        hexbyte.append(int(row_in[5], 16))
        hexbyte.append(int(row_in[6], 16))
        hexbyte.append(int(row_in[7], 16))
        hexbyte.append(int(row_in[8], 16))
        hexbyte.append(int(row_in[9], 16))
        hexbyte.append(int(row_in[10], 16))
        hexbyte.append(int(row_in[11], 16))
        hexbyte.append(int(row_in[12], 16))
        hexbyte.append(int(row_in[4], 16))
        hexbyte.append(int(0x58))
        if (row_in[13] == "标准帧"):
            hexbyte.append(0x00)
        else:
            hexbyte.append(0x01)
        if (row_in[14] == "数据帧"):
            hexbyte.append(0x00)
        else:
            hexbyte.append(0x01)
        hexbyte.append(util.get_sum(hexbyte) % 256)
        hexbyte.append(0x55)
        hexbyte.append(0x55)
        return hexbyte

    @staticmethod
    def getRamdom(start , end):
        return random.randint(start, end)
